using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class XmlDocumentPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCreateXmlFile_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDocument = new XmlDocument();
        XmlDeclaration xmlDeclaration = 
        xmlDocument.CreateXmlDeclaration("1.0",null,"yes");
        xmlDocument.InsertBefore(xmlDeclaration, 
        xmlDocument.DocumentElement);
        
        XmlComment xmlComment;
        xmlComment = xmlDocument.CreateComment(
        "Written by Pham Huu Khang " + 
        DateTime.Now.ToLongDateString());
        xmlDocument.InsertAfter(xmlComment, 
        xmlDeclaration);
        
        XmlElement xmlRootElement = 
        xmlDocument.CreateElement("root");
        xmlDocument.InsertAfter(xmlRootElement, xmlComment);

        XmlElement xmlElement;
        xmlElement = 
        xmlDocument.CreateElement("Book");         
        xmlElement.InnerText  = "ASP.NET 2.0";
        xmlRootElement.AppendChild(xmlElement);
        xmlElement =
        xmlDocument.CreateElement("Book");
        xmlElement.InnerText = "C# 2005";
        xmlRootElement.AppendChild(xmlElement);
        xmlElement =
        xmlDocument.CreateElement("Book");
        xmlElement.InnerText = 
        "Microsoft Office SharePoint Server 2007";
        xmlRootElement.AppendChild(xmlElement); 
        
         
        string fileName = Server.MapPath("XmlDocument.xml");
        xmlDocument.Save(fileName);
        
    }
    protected void btnAttribute_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDocument = new XmlDocument();        
        XmlDeclaration xmlDeclaration =
        xmlDocument.CreateXmlDeclaration("1.0", null, "yes");
        xmlDocument.InsertBefore(xmlDeclaration,
        xmlDocument.DocumentElement);

        XmlComment xmlComment;
        xmlComment = xmlDocument.CreateComment(
        "Written by Pham Huu Khang " +
        DateTime.Now.ToLongDateString());
        xmlDocument.InsertAfter(xmlComment,
        xmlDeclaration);

        XmlElement xmlRootElement =
        xmlDocument.CreateElement("root");
        xmlDocument.InsertAfter(xmlRootElement, xmlComment);

        XmlElement xmlElement;
        xmlElement =
        xmlDocument.CreateElement("Book");
        XmlAttribute xmlAttribute = 
        xmlDocument.CreateAttribute("BookID");
        xmlAttribute.Value = "1001";
        xmlElement.SetAttributeNode(xmlAttribute);        
        xmlElement.SetAttribute("Price","70.0000");
        xmlElement.InnerText = "ASP.NET 2.0";
        xmlRootElement.AppendChild(xmlElement);        

        string fileName = Server.MapPath("Attribute.xml");
        xmlDocument.Save(fileName);
    }
    protected void bnRead_Click(object sender, EventArgs e)
    {
        Response.Redirect("ReadData.aspx");
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDocument = new XmlDocument();
        string fileName = Server.MapPath("Books.xml");
        xmlDocument.Load(fileName);
        
        XmlElement newXmlElement =
        InsertNewNode(xmlDocument, 
        "ASP.NET-000-001", 
        "ASP.NET 2.0 - Unit 1", 
        "76.000", 
        "Education publisher");
        
        
        /*XmlElement newXmlElement = xmlDocument.CreateElement("book");
        newXmlElement.SetAttribute("BSVN","ASP.NET-000-001");          
        
        XmlElement xmlElement = xmlDocument.CreateElement("title");
        xmlElement.InnerText ="ASP.NET 2.0 - Unit 1" ;
        newXmlElement.InsertAfter(xmlElement, newXmlElement.LastChild);
        xmlElement = xmlDocument.CreateElement("price");
        xmlElement.InnerText = "76.000";
        newXmlElement.InsertAfter(xmlElement, newXmlElement.LastChild);
        xmlElement = xmlDocument.CreateElement("publisher");
        xmlElement.InnerText = "Education publisher";
        newXmlElement.InsertAfter(xmlElement, newXmlElement.LastChild);
        */
        XmlNode xmlNode = xmlDocument.LastChild;
        xmlNode.InsertAfter(newXmlElement, xmlNode.LastChild); 
        
         
        xmlDocument.Save(fileName);
        
    }

    XmlElement InsertNewNode(
    XmlDocument xmlDocument,    
    string isbn, string title, 
    string price, string publisher)
    {
        XmlElement newXmlElement = 
                xmlDocument.CreateElement("book");
        newXmlElement.SetAttribute("ISBN", isbn);   
        
        XmlElement xmlElement = 
            xmlDocument.CreateElement("title");
        xmlElement.InnerText = title;
        newXmlElement.InsertAfter(xmlElement, 
            newXmlElement.LastChild);
        xmlElement = xmlDocument.CreateElement("price");
        xmlElement.InnerText = price;
        newXmlElement.InsertAfter(xmlElement, 
            newXmlElement.LastChild);
        xmlElement = xmlDocument.CreateElement("publisher");
        xmlElement.InnerText = publisher;
        newXmlElement.InsertAfter(xmlElement, 
            newXmlElement.LastChild);    
        return newXmlElement;
    }
    
    protected void Button3_Click(object sender, EventArgs e)
    {
        XmlDocument xmlDocument = new XmlDocument();
        string fileName = Server.MapPath("Books.xml");
        xmlDocument.Load(fileName);

        XmlElement newXmlElement =
        InsertNewNode(xmlDocument,
        "ASP.NET-000-002",
        "ASP.NET 2.0 - Unit 2",
        "75.000",
        "Education publisher");

        XmlNodeList xmlNodeList =
        xmlDocument.GetElementsByTagName("book");        
        XmlNode xmlNodeIndex = GetNode(
        xmlNodeList, "ISBN", "11-000000-002");
        xmlDocument.DocumentElement.InsertAfter(newXmlElement, 
        xmlNodeIndex ); 
        xmlDocument.Save(fileName);
    }
    
    XmlNode GetNode(XmlNodeList xmlNodeList, 
    string xmlNodeName, string xmlNodeValue)
    {          
        XmlNode xmlNodeIndex = null;
        foreach (XmlNode xmlNode in xmlNodeList)
        {
            if(xmlNode.Attributes[xmlNodeName].Value
            == xmlNodeValue)
            {
                xmlNodeIndex= xmlNode;
                break;
            }             
        }
        return xmlNodeIndex;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Response.Redirect("UpdatePage.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("DeletePage.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("SearchPage.aspx");
    }
}
